package com.aooci.ordercat.desktop.view;

import java.net.URL;
import java.util.ResourceBundle;

import com.aooci.ordercat.desktop.OrdercatMain;
import com.aooci.ordercat.desktop.SystemConstant;
import com.aooci.ordercat.domain.DiningTable;
import com.aooci.ordercat.vo.OperatedOrderVo;
import com.aooci.ordercat.vo.TakeawayVo;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

@SuppressWarnings("restriction")
public class SelectingNofClientPanelController implements Initializable {
	private OrdercatMain ordercatMain;
	private DashboardController dashboardController;
	private DiningTable table;
	private Button button;
	
	@FXML private Label numberOfClient;
	@FXML private Button plusNumberButton;
	@FXML private Button minusNumberButton;
	@FXML private Button createOrderButton;
	@FXML private Button closeButton;

	public void initialize(URL arg0, ResourceBundle arg1) {
		// TODO Auto-generated method stub
		this.numberOfClient.setText(SystemConstant.DEFAULT_NOF_CLIENT_FOR_ORDER + "");
		
		this.plusNumberButton.setOnAction((event) -> {
			numberOfClient.setText(String.valueOf(Integer.parseInt(numberOfClient.getText()) + 1));
		});
		
		this.minusNumberButton.setOnAction((event) -> {
			int number = Integer.parseInt(numberOfClient.getText());
			if(number > 1){
				number -= 1;
				numberOfClient.setText(String.valueOf(number));
			}
		});
		
		this.closeButton.setOnAction((event) -> {
			((Stage) this.closeButton.getScene().getWindow()).close();
		});
		
		this.createOrderButton.setOnAction(new EventHandler<ActionEvent>() {
		    @Override public void handle(ActionEvent actionEvent) {
//				this.operatedOrderVo = this.orderOperationService.createDiningTableOrder(diningTable.getId(), diningTable.getNofClient());
//				this.checkoutVo = this.orderOperationService.loadCheckoutVo(this.operatedOrderVo.getOrdercat().getId());
				//button.setBackground(new Background(new BackgroundFill(Color.BLUEVIOLET, CornerRadii.EMPTY, Insets.EMPTY)));
		    	button.getStyleClass().remove("dininghall_free_table");
		    	button.getStyleClass().add("dininghall_occupied_table");
				OperatedOrderVo operatedOrderVo = ordercatMain.getOrderOperationService().createDiningTableOrder(table.getId(), Integer.parseInt(numberOfClient.getText()));
				//TakeawayVo takeawayVo = new TakeawayVo(operatedOrderVo.getTakeaway());
				//this.checkoutVo = ordercatMain.getOrderOperationService().loadCheckoutVo(operatedOrderVo.getOrdercat().getId());
//				dashboardController.getTakeawayTable().getItems().add(takeawayVo);
				dashboardController.openOrderOperationDialog(operatedOrderVo);
				((Stage)((Node)actionEvent.getSource()).getScene().getWindow()).close();
		    }
		});
	}
	

	public void setOrdercatMain(OrdercatMain ordercatMain) {
		this.ordercatMain = ordercatMain;
	}

	public void setDashboardController(DashboardController dashboardController) {
		this.dashboardController = dashboardController;
	}


	public void setTable(DiningTable table) {
		this.table = table;
	}


	public void setButton(Button button) {
		this.button = button;
	}
}
